import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * author: MaHX
 * date: 2019/9/17/22:42
 * description:
 **/
public class TestCallable {
    public static void main(String args[]){
        CallableDemo callableDemo = new CallableDemo();
        //1,执行Callable方法，需要FutureTask类支持，用于接收运算结果
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callableDemo);
        new Thread(futureTask).start();
        //2，接收线程运算后的结果，用于闭锁
        try {
            int sum = futureTask.get();
            System.out.println(sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    static class CallableDemo implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            int sum = 0;
            for (int i = 0; i <= 100000; i++) {
                sum += i;
            }
            return sum;
        }
    }
}
